// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

// import Vue from 'vue'
import App from './App'
import router from './router'

import ff from '@/common/index.js'
Vue.prototype.ff = ff

import store from './store'
Vue.prototype.store = store

Vue.prototype.goid = function(url,id){
  router.push({
    path:url,
    query:{
      id:id
    }
  })
}

import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

import VueCropper from 'vue-cropper'
Vue.use(VueCropper)

// import all from '@/common/http.js'
// console.log(all)

// axios.defaults.baseURL = "http://222.173.219.2:9999"
if (process.env.NODE_ENV === "development") {
  axios.defaults.baseURL = '/apis';
}

// return new Promise((resolve, reject) =>{
//     axios.get(url, {
//         params: params
//     })
//     .then(res => {
//         resolve(res.data);
//     })
//     .catch(err => {
//         reject(err.data)
//     })
// });

// axios.interceptors.request.use(function (config) {
//     //拿一下token
//     const token = store.state.token

//     if(token){
//       config.headers.token = token
//     }else{
//       if(localStorage.getItem('token')){
//         config.headers.token = localStorage.getItem('token')
//         store.commit('addtoken',localStorage.getItem('token'));
//       }else{
//         // router.push('/login')
//       }
//     }
//     return config;
// }, function (error) {
//     return Promise.reject(error);
// });
// 响应拦截器
axios.interceptors.response.use(
    response => {
        if (response.status === 200) {
          if(response.data.msg.code == 401 || response.data.msg.code == 474){
            if(router.history.current.path == '/login'){
              return Promise.resolve(response);
            }else{
              router.replace({
                  path: '/login',
                  // query: {
                  //     redirect: router.currentRoute.fullPath
                  // }
              });
            }

          }

          else{
            return Promise.resolve(response);
          }

        } else {
            return Promise.reject(response);
        }
    }
);

// axios.defaults.baseURL = 'https://www.baidu.com';}
// Vue.prototype.get = get
// Vue.prototype.post = post

Vue.config.productionTip = false

// import peizhis from '@/assets/peizhi.js'
// Vue.prototype.peizhis = peizhis

import {copy,go,openw,back,duwenjian} from './assets/changyong.js'
Vue.prototype.copy = copy
Vue.prototype.go = go
Vue.prototype.openw = openw
Vue.prototype.duwenjian = duwenjian
Vue.prototype.back = function(){
  router.go(-1)
}

import bus from '@/assets/bus.js'
Vue.prototype.$bus = bus

const keyStr = "ofGlyFgu9bL2J7VQ"
Vue.prototype.decrypt = function(word){
  var key  = CryptoJS.enc.Utf8.parse(keyStr);//Latin1 w8m31+Yy/Nw6thPsMpO5fg==
  var decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7},1);
  return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
// ELEMENT.Message.success('nb')

router.beforeEach((to, from, next) => {

  // if(store.state.luyou){
  //   next()
  // }else{
  //   if(store.state.token){
  //     //authme 获取权限
  //     axios.get(`https://mock.apifox.cn/m1/2626903-0-default/authme?apifoxApiId=76964109?token=`+store.state.token)
  //       .then(response => {
  //           let quanxian = response.data.data.func

  //           if(quanxian.includes('A')){
  //             router.addRoute({ path: '/aihao', name: 'aihao', component: () => import('@/pages/aihao') })
  //           }
  //           if(quanxian.includes('B')){
  //             router.addRoute({ path: '/aihao2', name: 'aihao2', component: () => import('@/pages/aihao2') })
  //           }
  //           store.commit('addluyou')
  //           next()
  //       })
  //   }else{
  //     //local 拿token localStorage.getItem('token')
  //     let token = 'xxxx'
  //     axios.get(`https://mock.apifox.cn/m1/2626903-0-default/authme?apifoxApiId=76964109?token=`+token)
  //       .then(response => {
  //           let quanxian = response.data.data.func

  //           if(quanxian.includes('A')){
  //             router.addRoute({ path: '/aihao', name: 'aihao', component: () => import('@/pages/aihao') })
  //           }
  //           if(quanxian.includes('B')){
  //             router.addRoute({ path: '/aihao2', name: 'aihao2', component: () => import('@/pages/aihao2') })
  //           }
  //           store.commit('addluyou')
  //           next()
  //       })
  //   }

  // }


  next()
	/* 路由发生变化修改页面title */
	// if (to.path == '/admindingdan') {
	// 	if (JSON.parse(localStorage.getItem('SHOP')).shop_id == 50481 || JSON.parse(localStorage.getItem(
	// 			'SHOP')).shop_id == 301655) {
	// 		next()
	// 	} else {
	// 		next('/login')
	// 	}
	// } else {
	// 	next()
	// }

})

/* eslint-disable no-new */
new Vue({
	el: '#app',
	router,
  store,
	components: {
		App
	},
	template: '<App/>'
})
